<?php
// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\model\api;

use laytp\BaseModel;
use think\model\concern\SoftDelete;

class WorldGood extends BaseModel
{
    protected $name = 'world_good';

    protected $limit = 80;


    /**
     * 获取列表
     * @param $category
     * @param $sub_category
     * @param $keyword
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function list($category,$sub_category,$keyword){
        $where = [];
        if($category){
            $where[] = ['category', '=', $category];
        }
        if($keyword){
            $where[] = ['name', 'like', '%'.$keyword.'%'];
        }
        if($sub_category){
            $where[] = ['sub_category', '=', $sub_category];
        }
        $list = $this->where($where)
            ->field('id,category,sub_category,name,img')
            ->order('id asc')
            ->cache(true)
            ->paginate($this->limit)
            ->toArray();

        return $list;
    }


    /**
     * 详情
     * @param $good_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail($good_id){
        $info = $this->where('id','=',$good_id)->cache(true)->find()->toArray();
        $return = array(
            'id'   => $info['id'],
            'name' => $info['name'],
            'img'  => $info['img'],
            'big_img'        => $info['big_img'],
            'description'    => $info['description'],
            'base_attribute' => json_decode($info['base_attribute'],true),
        );
        if(in_array($info['category'],['食物','工具','战斗用品'])){
            $return['use']     = json_decode($info['use'],true);
            $return['get_way'] = json_decode($info['get_way'],true);
            $return['formula'] = json_decode($info['formula'],true);
        }else{
            switch($info['category']){
                case '方块':
                    $return['make_type']  = $info['make_type'];
                    $return['tool']       = json_decode($info['tool'],true);
                    $return['use']        = json_decode($info['use'],true);
                    $return['get_way']    = json_decode($info['get_way'],true);
                    $return['formula']    = json_decode($info['formula'],true);
                    $return['make_goods'] = json_decode($info['make_goods'],true);
                    break;
                case '生物':
                    $return['effect']     = json_decode($info['effect'],true);
                    $return['size']       = json_decode($info['size'],true);
                    $return['make_goods'] = json_decode($info['make_goods'],true);
                    $return['equipment']  = json_decode($info['equipment'],true);
                    $return['drop']       = json_decode($info['drop'],true);
                    $return['get_way']    = json_decode($info['get_way'],true);
                    $return['behavior']   = json_decode($info['behavior'],true);
                    break;
                case '魔咒':
                    $return['magic'] = (new WorldMagic())->field('name,img,effect')->cache(true)
                        ->select()->toArray();

                    $return['effect'] = $info['effect'];
                    $return['make_goods'] = json_decode($info['make_goods'],true);
                    $return['fuse_goods'] = json_decode($info['fuse_goods'],true);
                    break;
                case '状态':
                    $return['effect'] = $info['effect'];
                    $return['get_way']    = json_decode($info['get_way'],true);
                    $return['make_goods'] = json_decode($info['make_goods'],true);
                    break;
            }
        }

        return $return;
    }

}